/*
 * Author: 李波(Edwin Lee)
 * Date: 2024-08-19 08:09:36
 * FilePath: /mprpc/example/callee/friendservice.cc
 * LastEditTime: 2024-09-02 03:04:20
 * version: 1.0
 * Description: 
 */
#include "logger.h"
#include "friend.pb.h"
#include "mprpcapplication.h"
#include "rpcprovider.h"
#include <iostream>
#include <string>

class FriendService : public fixbug::FriendServiceRpc
{
public:
    std::vector<std::string> GetFriendsList(uint32_t userid)
    {
        std::cout << "do GetFriendsList service! userid: " << userid << std::endl;
        std::vector<std::string> vec;
        vec.push_back("zhang san");
        vec.push_back("li si");
        vec.push_back("wang wu");
        return vec;
    }

    void GetFriendsList(::google::protobuf::RpcController* controller,
                        const ::fixbug::GetFriendsListRequest* request,
                        ::fixbug::GetFriendsListResponse* response,
                        ::google::protobuf::Closure* done)
    {
        int32_t userid = request->userid();
        std::vector<std::string> friendslist = GetFriendsList(userid);
        response->mutable_result()->set_errcode(0);
        response->mutable_result()->set_errmsg("");
        // 输出repeated bytes
        for(std::string& name:friendslist)
        {
            std::string* p = response->add_friends();
            *p = name;
        }
        done->Run();
    }
private:
};

// 框架的使用
int main(int argc, char** argv)
{
    // 初始化框架
    std::cout << "init begin"<<std::endl;
    Logger& logger = Logger::getInstance();
    logger.setCache(false);
    logger.setDebug(true);
    LOG_INFO("hello friend!%s","zhangsan");
    MprpcApplication::init(argc,argv);

    // provider是一个rpc网络服务对象。把UserService对象发布到rpc节点上
    RpcProvider provider;
    provider.NotifyService(new FriendService());

    // 启动一个rpc服务发布节点 Run以后，进程进入阻塞状态，等待远程的rpc调用请求
    provider.Run();

    return 0;
}